package com.xinai.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.xinai.util.DBUtil;
import com.xinai.vo.SysDepartment;
import com.xinai.vo.SysUsers;

import java.util.List;

public class SysUsersDao extends BaseDao {

    private final String TABLE="sysUsers";

    public SysUsersDao(Context context){
        super(context);
    }

    //查询一条记录
    public SysUsers findFirst() {
        return callBack(TYPE_READ, new DaoCallBack<SysUsers>() {
            @Override
            public SysUsers invoke(SQLiteDatabase conn) {
                cursor = conn.query(TABLE, null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    SysUsers sysUsers = new SysUsers();
                    sysUsers.setId(cursor.getInt(cursor.getColumnIndex("id")));
                    sysUsers.setNo(cursor.getString(cursor.getColumnIndex("no")));
                    sysUsers.setUserName(cursor.getString(cursor.getColumnIndex("userName")));

                    return sysUsers;
                }
                return null;
            }
        });
    }



    public List<SysUsers> getList(final String parma) {
        return callBack(TYPE_READ, new DaoCallBack<List<SysUsers>>() {
            @Override
            public List<SysUsers> invoke(SQLiteDatabase conn) {
                String sql = " select * from sysUsers where [no] like ? or userName like ? ";
                cursor = conn.rawQuery(sql, new String[] {"%" + parma + "%", "%" + parma + "%"});
                if (cursor == null) {
                    return null;
                }
                List<SysUsers> departments = DBUtil.cursor2VOList(cursor, SysUsers.class);
                return departments;
            }
        });
    }

    //写入与修改记录
    public int insert(final SysUsers sysUsers) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                conn.execSQL("replace into sysUsers(id,[no],userName) values(?,?,?)",
                        new Object[] {sysUsers.getId(),sysUsers.getNo(),sysUsers.getUserName()});
                return 1;
            }
        });
    }


}
